//
//  WCLineHighlighter.m
//  WabbitCode
//
//  Created by William Towe on 2/12/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCLineHighlighter.h"
#import "WCMacros.h"
#import "WCAppController.h"
#import "WCRulerView.h"
#import "WCTextView.h"
#import "NSTextView+WCExtensions.h"
#import "NSString+WCExtensions.h"
#import <RegexKit/RegexKit.h>


@implementation WCLineHighlighter
#pragma mark *** Subclass Overrides ***
- (id)init {
	if (!(self = [super init]))
		return nil;
	
	
	return self;
}

- (void)dealloc {
	[NSObject cancelPreviousPerformRequestsWithTarget:self];

	[super dealloc];
}
#pragma mark *** Public Methods ***

+ (WCLineHighlighter *)sharedInstance; {
	static id _sharedInstance = nil;
	if (!_sharedInstance) {
		_sharedInstance = [[[self class] alloc] init];
	}
	return _sharedInstance;
}


- (void)unhighlightTextView:(WCTextView *)textView; {
	[[textView layoutManager] removeTemporaryAttribute:NSBackgroundColorAttributeName forCharacterRange:[[textView string] wholeRange]];
}

- (void)highlightTextView:(WCTextView *)textView; {
	[self highlightTextView:textView oldRange:[[textView string] wholeRange] newRange:[textView selectedRange]];
}
- (void)highlightTextView:(WCTextView *)textView oldRange:(NSRange)oldRange newRange:(NSRange)newRange; {
	NSString *string = [textView string];
	NSUInteger length = [string length];
	
	if (!length)
		return;
	
	if (NSMaxRange(oldRange) > length)
		return;
	
	[[textView layoutManager] removeTemporaryAttribute:NSBackgroundColorAttributeName forCharacterRange:[[textView string] wholeRange]];
	
	[[textView layoutManager] addTemporaryAttribute:NSBackgroundColorAttributeName value:NSUserDefaultsKeyedUnarchivedObjectForKey(kWCPrefsEditorLineHighlightColorKey) forCharacterRange:[string lineRangeForRange:newRange]];
}

@end
